import {ElementRef, Injectable} from '@angular/core';
import {HttpService} from "@core/http/http.service";
import * as API from "@api/index";
import {Observable} from "rxjs/Observable";

import {Response} from "@angular/http";
import {ToolsService} from "@shared/service/tools/tools.service";
import {ImageServeService} from "@shared/service/image/image-serve.service";
import {
  SNSResult, TrainClassDrillOutline, TrainClassroomScheme, TrainClassScheduleVO, TrainCourseTeachTemplate,
  TrainTeachInfoVo,
  TrainTeachProcess
} from "@interface/index";

// import {
//   TrainClassroomScheme, TrainClassScheduleVO, TrainTeachInfoVo,
//   TrainTeachProcess
// } from "@interface/train/train.model";


@Injectable()
export class PlanService {
  private _organizingFormsImg: Array<any>;
  constructor(
    private _httpService: HttpService,
    private _toolService: ToolsService,
    private _imgService: ImageServeService
  ) {
    this._organizingFormsImg = [
      {
        active: this._getImage('train/teach_type_1.png'),
        inactive: this._getImage('train/teach_type_1_n.png'),
        vaule: '1',
        isChecked: false
      },
      {
        active: this._getImage('train/teach_type_2.png'),
        inactive: this._getImage('train/teach_type_2_n.png'),
        vaule: '2',
        isChecked: false
      },
      {
        active: this._getImage('train/teach_type_3.png'),
        inactive: this._getImage('train/teach_type_3_n.png'),
        vaule: '3',
        isChecked: false
      },
      {
        active: this._getImage('train/teach_type_4.png'),
        inactive: this._getImage('train/teach_type_4_n.png'),
        vaule: '4',
        isChecked: false
      }
    ];
  }

  getOrganizingFormsImg(key): string {
    return this._organizingFormsImg[key - 1].active;
  }

  private  _getImage(src: string): string {
    return this._imgService.getImage(src);
  }
  /**
   * @description
   * 根据班级ID获取教学计划
   * @author @chenwh576
   * @time 2018.01.02
   * @param {number} classId
   * @return {Observable<SNSResult<TrainClassroomScheme>>}
   */
  fetchTeachScheme(classId: number): Observable<SNSResult<TrainClassroomScheme>> {
    const url = `${API.TRAIN_TEACH_SCHEME_CLASSID}?classId=${classId}`;
    return this._httpService.get(url).map((res: Response) => res.json())
      .filter(item => item.result === '0');
  }

  /**
   * @author guoqs1331
   * @time 2018/4/9
   * @description 获取教学目标
   */
  fetchDrilloutline(classId: number): Observable<SNSResult<TrainClassDrillOutline>>{
    const url = `${API.TRAIN_CLASSROOM_DRILLOUTLINE}?classId=${classId}`;
    return this._httpService.get(url).map((res: Response) => res.json())
      .filter(item => item.result === '0');
  }

  /**
   * @description
   * 根据班级ID获取 班级课时列表
   * @author @chenwh576
   * @time 2018.01.02
   * @param {number} classId
   * @return {Observable<SNSResult<TrainClassroomScheme>>}
   */
  fetchClassroomSchedule(classId: number): Observable<SNSResult<TrainClassScheduleVO[]>> {
    const url = `${API.TRAIN_CLASSROOM_SCHEDULE}?classId=${classId}`;
    return this._httpService.get(url).map((res: Response) => res.json())
      .filter(item => item.result === '0');
  }

  /**
   * @description
   * 保存教学计划
   * @author @chenwh576
   * @date 2018.01.02
   * @param {Object} data
   * @return {Observable<SNSResult<number>>}
   */
  saveTeachScheme(data: Object): Observable<SNSResult<number>> {
    const url = API.TRAIN_TEACH_SCHEME;
    return this._httpService.postUrlencode(url, data)
      .map((res: Response) => res.json());
  }

  /**
   * @description
   * 更新教学计划
   * @author @chenwh576
   * @date 2018.01.02
   * @param {Object} data
   * @return {Observable<SNSResult<number>>}
   */
  updateTeachScheme(data: Object): Observable<SNSResult<number>> {
    const url = API.TRAIN_TEACH_SCHEME;
    return this._httpService.putUrlencode(url, data)
      .map((res: Response) => res.json());
  }

  /**
   * @description 根据课时ID 获取教学信息
   * @author @chenwh576
   * @date 2018.01.02
   * @modify @ouxh862
   * @date 2019/1/31
   * @param {number} scheduleId
   * @return {Observable<SNSResult<TrainTeachInfoVo>>}
   */
  fetchTeachInfo(scheduleId: number): Observable<SNSResult<TrainTeachInfoVo>> {
    const url = `${API.TRAIN_TEACH_INFO_SCHEDULEID}?classScheduleId=${scheduleId}`;
    return this._httpService.get(url)
      .map((res: Response) => res.json());
  }
  /**
   * @description 根据课时ID 获取教学信息的模板
   * @author @chenwh576
   * @date 2018.01.02
   * @modify @ouxh862
   * @date 2019/1/31
   * @param {number} scheduleId
   * @return {Observable<SNSResult<TrainTeachInfoVo>>}
   */
  fetchScheduleIdTemplate(scheduleId: number): Observable<SNSResult<TrainCourseTeachTemplate>> {
    const url = `${API.TRAIN_TEACH_TEMPLATE_SCHEDULEID}?scheduleId=${scheduleId}`;
    return this._httpService.get(url)
      .map((res: Response) => res.json());
  }

  /**
   * @description 修改教学信息
   * @author @chenwh576
   * @date 2018/1/3 下午4:40
   * @params {Object} TrainTeachInfoVo
   * @return {Observable<SNSResult<number>>}
   */
  updateTeachInfo(data: TrainTeachInfoVo): Observable<SNSResult<number>> {
    const url = `${API.TRAIN_TEACH_INFO}`;
    return this._httpService.putUrlencode(url, data)
      .map((res: Response) => res.json());
  }

  /**
   * @description 保存教学信息
   * @author @chenwh576
   * @date 2018/1/3 下午4:40
   * @params {Object} data
   * @return {Observable<SNSResult<number>>}
   */
  saveTeachInfo(data: Object): Observable<SNSResult<number>> {
    const url = `${API.TRAIN_TEACH_INFO}`;
    return this._httpService.postUrlencode(url, data)
      .map((res: Response) => res.json());
  }


  // /**
  //  * @description 通过班级课时Id，查询教学过程信息
  //  * @author @chenwh576
  //  * @date 2018/1/3 下午4:40
  //  * @param {number} classScheduleId
  //  * @return {Observable<SNSResult<TrainTeachProcess>>}
  //  */
  // fetchTeachProcessByClassscheduleId(classScheduleId: number): Observable<SNSResult<TrainTeachProcess[]>> {
  //   const url = `${API.TRAIN_TEACH_PROCESS_CLASSSCHEDULEID}?classScheduleId=${classScheduleId}`;
  //   return this._httpService.get(url)
  //     .map((res: Response) => res.json());
  // }
  /**
   * @description 通过班级课时Id，查询教学过程信息
   * @author @chenwh576
   * @date 2018/1/3 下午4:40
   * @modify @ouxh862
   * @date 2019/1/31
   * @param {number} classScheduleId
   * @return {Observable<SNSResult<TrainTeachProcess>>}
   */
  fetchTeachProcessByClassscheduleId(classScheduleId: number): Observable<SNSResult<TrainTeachProcess[]>> {
    const url = `${API.TRAIN_TEACH_PROCESS_CLASSSCHEDULEID}?classScheduleId=${classScheduleId}`;
    return this._httpService.get(url)
      .map((res: Response) => res.json());
  }

  /**
   * @description 查询学生详情
   * @author @chenwh576
   * @date 2018/1/3 下午4:40
   * @param {number} classScheduleId
   * @return {Observable<SNSResult<TrainTeachProcess>>}
   */
  fetchTeachProcess(trainTeachProcessId: number): Observable<SNSResult<TrainTeachProcess>> {
    const url = `${API.TRAIN_TEACH_PROGRESS}?trainTeachProcessId=${trainTeachProcessId}`;
    return this._httpService.get(url)
      .map((res: Response) => res.json());
  }

  /**
   * @description 保存/修改教学过程信息
   * @author @chenwh576
   * @date 2018/1/3 下午4:40
   * @param {number} classScheduleId
   * @return {Observable<SNSResult<number>>}
   */
  saveTeachProcess(data: Object): Observable<SNSResult<number>> {
    const url = `${API.TRAIN_TEACH_PROGRESS}`;
    return this._httpService.postUrlencode(url, data)
      .map((res: Response) => res.json());
  }

  /**
   * @description 通过教学过程Id，删除教学过程
   * @author @chenwh576
   * @date 2018/1/3 下午4:40
   * @param {number} classScheduleId
   * @return {Observable<SNSResult<number>>}
   */
  deleteTeachProcess(trainTeachProcessId: number): Observable<SNSResult<number>> {
    const url = `${API.TRAIN_TEACH_PROGRESS}?trainTeachProcessId=${trainTeachProcessId}`;
    return this._httpService.delete(url)
      .map((res: Response) => res.json());
  }
  /**
   * 显示已经选中的li
   * @param ele 页面上的元素 ul
   * @param {string} target
   * @author ouxh862
   * @date 2018.01.16
   * @constructor
   */
  AdaptSelect(ele: ElementRef, target: string) {
    const focusFieldLength = ele.nativeElement.children.length;
    for (let i = 0; i < focusFieldLength; i++) {
      if (target.indexOf(ele.nativeElement.children[i].innerHTML) > -1) {
        ele.nativeElement.children[i].click();
      }
    }
  }

  /**
   * @description 获取课时计划
   * @author ouyqx-691
   * @date 2018-3-30
   * @param scheduleId : 课时ID
   */
  getLessonPlanInfo(scheduleId: number) {
    const url = `${API.TRAIN_TEARCHER_LESSON_PLAN}?scheduleId=${scheduleId}`;
    return this._httpService.get(url)
      .map((res: Response) => res.json());
  }
  /**
   * @description 获取课时教学计划详情
   * @author ouyqx-691
   * @date 2018-4-2
   * @param scheduleId : 课时ID
   */
  getLessonPlanInfoDetail(scheduleId: number) {
    const url = `${API.TRAIN_TEACHER_LESSON_PLAN_DETAIL}?styleId=${scheduleId}`;
    return this._httpService.get(url)
      .map((res: Response) => res.json());
  }
}
